﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ClientDataAccess;
using SquadPlacer;
using System.Threading;
using CustomControls;
using System.Net.Mail;

namespace WinFormClient
{
    static class Program
    {
        static Game1 Game;
        static LoginForm LogForm = null;
        static MainForm MainForm = null;
        static DBO.Utility.PLICCredentials Credentials = null;
        static ClientSessionHandler SessionHandler = null;
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            SessionHandler = new ClientSessionHandler();
            Credentials = new DBO.Utility.PLICCredentials();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            LogForm = new LoginForm();
            LogForm.CenterForm();
            LogForm.Content = Credentials;
            MainForm = new MainForm();

            LogForm.onLogIn += new EventHandler(LogForm_onLogIn);
            LogForm.onPassSend += new EventHandler(LogForm_onPassSend);

            Game = new Game1(MainForm.getDrawingSurface());

            Game.onAddTroop += new EventHandler(Game_onAddTroop);
            Game.onDeleteTroop += new EventHandler(Game_onDeleteTroop);

            LogForm.Show();
            MainForm.Game = Game;
            Game.Run();
        }

        static void LogForm_onPassSend(object sender, EventArgs e)
        {
            PasswordForgottenForm Form = (PasswordForgottenForm)sender;

            try
            {
                SessionHandler.ChangePassword(Form.Name, new MailAddress(Form.Address));

            }
            catch (Exception ex)
            {
                MessageBox.Show("Mail address was not valid, please try again.");
            }
        }

        static void Game_onDeleteTroop(object sender, EventArgs e)
        {
            MainForm.ReplenishTroop((string)sender);
        }

        static void Game_onAddTroop(object sender, EventArgs e)
        {
            if (MainForm.ConsumeTroop((string)sender))
                Game.entity_name = null;
        }

        static void Run(string token)
        {
            MainForm.Player = SessionHandler.GetPlayerInfo(token);

            MainForm.FormClosed += new FormClosedEventHandler(MainForm_FormClosed);

            MainForm.Show();
            Game1.MouseEnabled = true;
            MainForm.onClose += new EventHandler(MainForm_onClose);
            MainForm.onSelectionChanged += new EventHandler(MainForm_onSelectionChanged);
        }

        static void MainForm_onSelectionChanged(object sender, EventArgs e)
        {
            Game.entity_name = (string)sender;
        }

        static void MainForm_onClose(object sender, EventArgs e)
        {
            Game.Exit();
        }

        static void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LogForm.Close();
            LogForm.Dispose();
        }

        static void LogForm_onLogIn(object sender, EventArgs e)
        {
            string token = null;
            if (Credentials.Login == "" || Credentials.Password == "")
                LogForm.Error = "Login and Password fields must not be empty.";
            else if ((token = SessionHandler.TryLogin(Credentials)) != null)
            {
                LogForm.Hide();
                Run(token);
            }
            else
                LogForm.Error = "Login failed. Please try again.";
        }
    }
}
